/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.domain;

import java.util.Date;

import td.domain.util.StatisticalComparison;

import com.googlecode.objectify.annotation.Entity;

/**
 * A category allows to group topics in a hierarchical manner.
 * 
 * @author Artem Shamsutdinov
 * 
 */
@Entity
public class Category extends Hierarchy<Category> {

	private static final long serialVersionUID = -7892012989203822285L;

	public static StatisticalComparison currentComparisonType = StatisticalComparison.NUMBER_OF_THOUGHT_READS;

	private Date lastTopicAdded;
	private Long directNumTags = 0L;

	public Category() {
		super();
	}

	public Category(String name) {
		super(name);
	}

	public Category(String name, String description, Category parentCategory) {
		super(name, description, parentCategory);
	}

	@Override
	protected Category getInstance(String name, String description,
			Category parent) {
		return new Category(name, description, parent);
	}

	public Date getLastTopicAdded() {
		return lastTopicAdded;
	}

	public void setLastTopicAdded(Date lastTopicAdded) {
		this.lastTopicAdded = lastTopicAdded;
	}

	public Long getDirectNumTags() {
		return directNumTags;
	}

	public void setDirectNumTags(Long directNumTags) {
		this.directNumTags = directNumTags;
	}

	@Override
	public StatisticalComparison getComparisonType() {
		return currentComparisonType;
	}

	@Override
	public int compareTo(Statistical otherStats) {
		if (otherStats instanceof Category) {
			Category otherCategory = (Category) otherStats;
			switch (getComparisonType()) {
			case DIRECT_NUMBER_OF_TAGS:
				return otherCategory.directNumTags.compareTo(directNumTags);
			default:
				return super.compareTo(otherCategory);
			}
		}
		throw new RuntimeException(
				"Categories should only be compared to other categories");
	}

	/* (non-Javadoc)
	 * @see td.domain.StatisticalGrouping#getTypeForUrl()
	 */
	@Override
	public String getTypeForUrl() {
		return GroupType.CATEGORY.name();
	}
}
